﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LearnNet.BLL;
namespace Advertise
{
    public partial class frmUserList : Form
    {
        public frmUserList()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            bing();
        }
        private void bing()
        {
            DataTable dt = new DataTable();
            dt = LearnNet.DAL.DALHelper.LearnNetHelper.ExecuteQuery(CommandType.Text, "select id,name ,website, pwd,email from userinfo order by website", null);
            dataGridView1.DataSource = dt;
            if (dt.Rows.Count > 0)
            {
                dataGridView1.Columns[0].ReadOnly = true;
            }
            dt.Dispose();
        }

        private void 登录选中用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号

            //dataGridView1[1, rowIndex].Value.ToString();//获得你要的行和列的数据
            if (dataGridView1[1, rowIndex].Value.ToString() == "" || dataGridView1[2, rowIndex].Value.ToString() == "" ||dataGridView1[3, rowIndex].Value.ToString() == "")
            {
                MessageBox.Show("用户名或密码或类型不能为空！");
                return;
            }
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.regEntity.Name = dataGridView1[1, rowIndex].Value.ToString();
            frmLogin.regEntity.Pwd = dataGridView1[3, rowIndex].Value.ToString();
            frmLogin.regEntity.Type = dataGridView1[2, rowIndex].Value.ToString();
            frmLogin.Show();
            
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;

            //dataGridView1[1, rowIndex].Value.ToString();//获得你要的行和列的数据

            if (dataGridView1[1, rowIndex].Value.ToString() == "")
            {
                MessageBox.Show("请填写名称");
                return;
            }
            if (dataGridView1[3, rowIndex].Value.ToString() == "")
            {
                MessageBox.Show("请填写密码");
                return;
            }
            if (dataGridView1[2, rowIndex].Value.ToString() == "")
            {
                MessageBox.Show("请填写类型");
                return;
            }
            int count = 0;
            try
            {

                string str = string.Empty;
                str = "update UserInfo set name ='" + dataGridView1[1, rowIndex].Value.ToString() + "' , website='" + dataGridView1[2, rowIndex].Value.ToString() + "',pwd='" + dataGridView1[3, rowIndex].Value.ToString() + "' where id=" + dataGridView1[0, rowIndex].Value.ToString();
               
                LearnNet.DAL.DALHelper.LearnNetHelper.ExecuteNonQuery(CommandType.Text, str, null);
   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
              
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号
            if (MessageBox.Show("确定要删除" + dataGridView1[1, rowIndex].Value.ToString(),"删除确定",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk) == DialogResult.OK)
            {
                try
                {
                    string str = string.Empty;
                    str = "delete from UserInfo  where id=" + dataGridView1[0, rowIndex].Value.ToString();

                    LearnNet.DAL.DALHelper.LearnNetHelper.ExecuteNonQuery(CommandType.Text, str, null);
                    bing();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAdd add = new FrmAdd();
            add.ShowDialog();
            bing();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Clipboard.SetText(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bing();
        }


    }
}
